package com.gxa.xiaoucomeshome.controller;

import com.gxa.xiaoucomeshome.pojo.User;
import com.gxa.xiaoucomeshome.service.UserManagerService;
import com.gxa.xiaoucomeshome.vo.UserManagerVo;
import com.gxa.xiaoucomeshome.vo.UserOrderDetailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 */
@Api(value = "ExportUserManageController",tags = "导出用户文件方法")
@Controller
@RequestMapping("/export")
public class ExportUserManageController{

    @Autowired
    UserManagerService userManagerService;

    @GetMapping("/export")
    @ApiOperation(value = "导出用户信息方法")
    public void export(@RequestParam(value = "userManage") String userManage,
                       @ApiIgnore HttpServletResponse response) throws Exception {
//        List<User> all = new ArrayList<>();
        List<UserManagerVo> all = userManagerService.selectAllByPage();
        // 工作簿对象
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 创建工作表
        HSSFSheet sheet = workbook.createSheet("用户订单列表");
        String fileName = "用户订单列表.xls";
        // 将文件名中的中文部分被URL编码后的内容进行解析, 最终还是显示为中文状态
        fileName = URLEncoder.encode(fileName, "utf-8").replaceAll("\\+", "%20");
        // headers表示excel表中第一行的表头 在excel表中添加表头
        String[] headers = { "编号", "姓名", "电话","最后活跃时间","配送订单数","所属站点","状态",};
        // 创建一个行对象, 作为表头
        HSSFRow row = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);
            // 在行中添加单元格
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
            // 把字段名添加到表头行上
        }
        //准备将时间格式处理为标准日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 添加数据行:
        int rowNum = 1;
        //zhe all 是空
        for (UserManagerVo item : all) {
            HSSFRow dataRow = sheet.createRow(rowNum);
            dataRow.createCell(0).setCellValue(rowNum);
            dataRow.createCell(1).setCellValue(item.getName());
            dataRow.createCell(2).setCellValue(item.getPhone());
            dataRow.createCell(3).setCellValue(dateFormat.format(item.getCommitdate()));
            dataRow.createCell(4).setCellValue(item.getOrder_id());
            dataRow.createCell(5).setCellValue(item.getSite_name());
            dataRow.createCell(6).setCellValue(item.getState());
            rowNum++;
        }
        //返回给前端下载
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }


    @GetMapping("/selectUserManageDetail")
    @ApiOperation(value = "导出所有订单信息方法")
    public void selectUserManageDetail(@RequestParam(value = "selectUserDetail") String selectUserDetail,
                       @ApiIgnore HttpServletResponse response) throws Exception {
//        List<User> all = new ArrayList<>();

        List<UserOrderDetailVo> all = userManagerService.selectAllOrder();
        // 工作簿对象
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 创建工作表
        HSSFSheet sheet = workbook.createSheet("订单列表");
        String fileName = "用户订单列表.xls";
        // 将文件名中的中文部分被URL编码后的内容进行解析, 最终还是显示为中文状态
        fileName = URLEncoder.encode(fileName, "utf-8").replaceAll("\\+", "%20");
        // headers表示excel表中第一行的表头 在excel表中添加表头
        String[] headers = { "订单编号", "货物类别", "商品数量","箱数","金额","重量","订单状态","收件人",
                "联系方式","配送人","配送时间","时效产品","所属站点"};
        // 创建一个行对象, 作为表头
        HSSFRow row = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);
            // 在行中添加单元格
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
            // 把字段名添加到表头行上
        }
        //准备将时间格式处理为标准日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 添加数据行:
        int rowNum = 1;
        //zhe all 是空
        for (UserOrderDetailVo item : all) {
            HSSFRow dataRow = sheet.createRow(rowNum);
            dataRow.createCell(0).setCellValue(rowNum);
            dataRow.createCell(1).setCellValue(item.getOrder_id());
            dataRow.createCell(2).setCellValue(item.getCommodity_name());
            dataRow.createCell(3).setCellValue(item.getCommodity_num());
            dataRow.createCell(4).setCellValue(item.getBoxes());
            dataRow.createCell(5).setCellValue(item.getMoney());
            dataRow.createCell(6).setCellValue(item.getWeight());
            dataRow.createCell(7).setCellValue(item.getState());
            dataRow.createCell(8).setCellValue(item.getName());
            dataRow.createCell(9).setCellValue(item.getPhone());
            dataRow.createCell(10).setCellValue(item.getConame());
            dataRow.createCell(11).setCellValue(item.getDispatch_time());
            dataRow.createCell(12).setCellValue(item.getAging_name());
            dataRow.createCell(13).setCellValue(item.getSite_name());
            rowNum++;
        }
        //返回给前端下载
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }
}
